package com.itheima.controller;

import com.itheima.dto.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.POIUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/ordersetting")
public class OrderSetingController {
    @Autowired
    private OrderSettingService orderSettingService;
//根据月份查询数据
    @PostMapping("/getOrderSettingByMonth")
    public Result getOrderSettings(String date) {
        List<Map<String, Object>> maps = orderSettingService.getOrderSettingByDate(date);
        return Result.success(maps, "获取预约设置数据成功");

    }

    @PostMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) throws ParseException {

        orderSettingService.editNumberByDate(orderSetting);
        return Result.success("修改成功");
    }
   //导入excel
    @PostMapping("/upload")
    public Result uploadOrderSetting(@RequestParam("excelFile") MultipartFile excelFile) {

        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            List<OrderSetting> orderSettingList = new ArrayList<>();
            for (String[] strings : list) {
                String OrderDate = strings[0];
                String number = strings[1];
                String replace = OrderDate.replace("/", "-");
                Date date = DateUtils.parseString2Date(replace);
                OrderSetting orderSetting = new OrderSetting(date, Integer.parseInt(number));
                orderSettingList.add(orderSetting);
            }
            orderSettingService.add(orderSettingList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("导入失败");
        }
        return Result.success("导入成功");
    }

}
